<?

class CLFetchers{

  /**
   * cash bank type
   * CASH, BANK or GIRO
   *
   * @param Database  $Db
   * @param string    $cbCode
   * @param string    $srcDb
   * @param string    $cbTxType
   */
  function fetchCbTxType(&$Db,
                         &$cbCode,
                          $srcDb,
                         &$cbTxType){

    $query  = "SELECT *
               FROM ".$srcDb."
               WHERE  C_B_CODE='".$cbCode."' ";

    $Db->execFetchQuery($query,
                        $nrows,
                        $result);

    $cbTxType = $result["C_B_TYPE"][0];
  }

  /**
   * source Cash Bank header details
   *
   * @param Database  $Db
   * @param string    $refId
   * @param string    $srcDb
   * @param integer   $nHeader
   * @param array     $resHeader
   */
  function fetchSourceHeader(&$Db,
                             &$refId,
                              $srcDb,
                             &$nHeader,
                             &$resHeader){

    $query  = "SELECT *
               FROM ".$srcDb."
               WHERE  REFERENCE='".$refId."'
                      AND STATUS='"._ACTIVE."' ";

    $Db->execFetchQuery($query,
                        $nHeader,
                        $resHeader);
  }

  /**
   * outstanding' remain to be paid
   * with generic condition
   *
   * @param Database  $Db
   * @param string    $condt
   * @param string    $suppCode
   * @param string    $periode
   * @param string    $refId
   * @param string    $srcDb
   * @param integer   $nRemain
   * @param array     $resRemain
   */
  function fetchOutstandingRemain2(&$Db,
                                    $condt,
                                   &$suppCode,
                                   &$periode,
                                   &$refId,
                                    $srcDb,
                                   &$nRemain,
                                   &$resRemain){

    $query  = "SELECT SUM(AMOUNT) AS REMAIN
               FROM ".$srcDb."
               WHERE  ".$condt."='".$refId."'
                      AND PERIODE=".$periode." ";

    $Db->execFetchQuery($query,
                        $nRemain,
                        $result);

    $query  = "SELECT TR_DATE,
                      DUE_DATE
               FROM ".$srcDb."
               WHERE  ".$condt."='".$refId."'
                      AND PERIODE=".$periode." ";

    $Db->execFetchQuery($query,
                        $nrows,
                        $resRemain);

    /**
     * copy the remain to this tx
     */
    $resRemain["REMAIN"] = $result["REMAIN"];

  }

  /**
   * outstanding' remain to be paid
   *
   * @param Database  $Db
   * @param string    $suppCode
   * @param string    $periode
   * @param string    $refId
   * @param string    $srcDb
   * @param integer   $nRemain
   * @param array     $resRemain
   */
  function fetchOutstandingRemain(&$Db,
                                  &$suppCode,
                                  &$periode,
                                  &$refId,
                                   $srcDb,
                                  &$nRemain,
                                  &$resRemain){

    $query  = "SELECT SUM(AMOUNT) AS REMAIN
               FROM ".$srcDb."
               WHERE  REFERENCE='".$refId."'
                      AND PERIODE=".$periode." ";

    $Db->execFetchQuery($query,
                        $nRemain,
                        $result);

    $query  = "SELECT TR_DATE,
                      DUE_DATE
               FROM ".$srcDb."
               WHERE  REFERENCE='".$refId."'
                      AND PERIODE=".$periode." ";

    $Db->execFetchQuery($query,
                        $nrows,
                        $resRemain);

    /**
     * copy the remain to this tx
     */
    $resRemain["REMAIN"] = $result["REMAIN"];

  }

  /**
   * Dp outstanding
   *
   * @param Database  $Db
   * @param string    $suppCode
   * @param string    $periode
   * @param string    $srcDb
   * @param integer   $nOutstanding
   * @param array     $outstanding
   */
  function fetchDpOutstanding(&$Db,
                              &$suppCode,
                              &$periode,
                               $srcDb,
                              &$nOutstanding,
                              &$outstanding){

    $outstanding = array("REFERENCE" => array(),
                         "REMAIN"    => array(),
                         "TR_DATE"   => array(),
                         "DUE_DATE"  => array());

    $condt = " AND TX_TYPE NOT IN ("._CASH_IN.","._BANK_IN.","._GIRO_IN.")";

    $query  = "SELECT DISTINCT REFERENCE
               FROM ".$srcDb."
               WHERE  PERIODE=".$periode."
                      AND SUPPLIER_CODE='".$suppCode."'
                      ".$condt;

    $Db->execFetchQuery($query,
                        $nJid,
                        $resJid);

    /**
     * foreach source JID
     * extract the remain
     */
    for($iJid = 0; $iJid < $nJid ; $iJid++){

      $this->fetchOutstandingRemain2($Db,
                                     "SOURCE",
                                     $suppCode,
                                     $periode,
                                     $resJid["REFERENCE"][$iJid],
                                     $srcDb,
                                     $nRemain,
                                     $resRemain);

      if($resRemain["REMAIN"][0] > 0){

        array_push($outstanding["REFERENCE"], $resJid["REFERENCE"][$iJid]);

        array_push($outstanding["REMAIN"], $resRemain["REMAIN"][0]);

        array_push($outstanding["TR_DATE"], $resRemain["TR_DATE"][0]);

        array_push($outstanding["DUE_DATE"], $resRemain["DUE_DATE"][0]);
      }else{

        /* Do nothing */
      }
    }

    $nOutstanding = sizeof($outstanding["REFERENCE"]);
  }

  /**
   * Dp issued for this supplier within
   * this periode
   *
   * @param Database  $Db
   * @param string    $suppCode
   * @param string    $srcDpOutRefId
   * @param string    $periode
   * @param string    $srcDb
   * @param integer   $nIssued
   * @param array     $issued
   */
  function fetchDpIssued(&$Db,
                         &$suppCode,
                         &$srcDpOutRefId,
                         &$periode,
                          $srcDb,
                         &$nIssued,
                         &$issued){

    $issued = array("REFERENCE" => array(),
                    "AMOUNT"    => array(),
                    "TR_DATE"   => array(),
                    "DUE_DATE"  => array());

    $query  = "SELECT DISTINCT H.REFERENCE
               FROM ".$srcDb." H
               WHERE  H.PERIODE=".$periode."
                      AND H.REFERENCE<>H.SOURCE
                      AND SUPPLIER_CODE='".$suppCode."' ";

    $Db->execFetchQuery($query,
                        $nJid,
                        $resJid);

    /**
     * foreach source JID
     * extract the amount
     */
    for($iJid = 0; $iJid < $nJid ; $iJid++){

      /**
       * fetch the amount of tx which used the dp
       */
      $query	= "SELECT *
                 FROM ".$srcDb."
                 WHERE	P.REFERENCE='".$resJid["REFRENCE"][$iJid]."' ";

      $Db->execFetchQuery($query,
                          $nIssued,
                          $resIssued);


      if($resIssued["AMOUNT"][0] > 0){

        array_push($issued["REFERENCE"], $resJid["REFERENCE"][$iJid]);

        array_push($issued["AMOUNT"], $resIssued["AMOUNT"][0]);

        array_push($issued["TR_DATE"], $resIssued["TR_DATE"][0]);

        array_push($issued["DUE_DATE"], $resIssued["DUE_DATE"][0]);
      }else{

        /* Do nothing */
      }
    }

    $nIssued = sizeof($issued["REFERENCE"]);
  }

  /**
   * Sub detail of DP
   *
   * @param Database  $Db
   * @param string    $tmpJid
   * @param string    $srcDb
   * @param integer   $nDetail
   * @param array     $lstDetail
   */
  function fetchSubDp(&$Db,
                       $tmpJid,
                       $srcDb,
                      &$nDetail,
                      &$lstDetail){

    $query  = "SELECT *
               FROM ".$srcDb."
               WHERE  JOB_ID='".$tmpJid."'
               ORDER BY NO ASC";

    $Db->execFetchQuery($query,
                        $nDetail,
                        $lstDetail);
  }

  /**
   * Detail of DP
   *
   * @param Database  $Db
   * @param string    $tmpJid
   * @param string    $srcDb
   * @param integer   $nDetail
   * @param array     $lstDetail
   */
  function fetchDtlDp(&$Db,
                       $tmpJid,
                       $srcDb,
                      &$nDetail,
                      &$lstDetail){

    $query	= "SELECT *
               FROM ".$srcDb."
               WHERE	JOB_ID='".$tmpJid."' ";

    $Db->execFetchQuery($query,
                        $nDetail,
                        $lstDetail);
  }

  /**
   * list of detail
   *
   * @param Database  $Db
   * @param string    $jobId
   * @param string    $srcDb
   * @param integer   $nDetail
   * @param array     $lstDetail
   */
  function fetchLstDataDetail(&$Db,
                              &$jobId,
                               $srcDb,
                              &$nDetail,
                              &$lstDetail){

    $query	= "SELECT *
               FROM ".$srcDb."
               WHERE	JOB_ID='".$jobId."' ";

    $Db->execFetchQuery($query,
                        $nDetail,
                        $lstDetail);
  }


  /**
   * converted qty
   *
   * @param Database $Db
   * @param string   $modelName
   * @param string   $to
   * @param integer  $toQty
   * @param string   $from
   * @param Composer $SourceDb
   * @param integer  $cvtQty
   */
  function fetchConvertedQty(&$Db,
                             &$modelName,
                             &$to,
                             &$toQty,
                             &$from,
                             &$SourceDb,
                             &$cvtQty){

    $query	= "SELECT C.QTY
               FROM ".$SourceDb->fetchValue("srcDbMstModel")."      M,
                    ".$SourceDb->fetchValue("srcDbMstConvertion")." C
               WHERE	M.MODEL='".$modelName."'
                      AND C.MODEL_ID=M.MODEL_ID
                      AND C.UNIT_NAME='".$from."'
                      AND C.UNIT_TO='".$to."' ";

    $Db->execFetchQuery($query,
                        $nrows,
                        $result);

    $cvtQty = (int)$result["QTY"][0] * (int)$toQty;
  }

  /**
   * model unit from and to
   *
   * @param Database $Db
   * @param string   $modelName
   * @param Composer $SourceDb
   * @param array    $unitFrom
   * @param array    $unitTo
   */
  function fetchModelUnit(&$Db,
                          &$modelName,
                          &$SourceDb,
                          &$unitFrom,
                          &$unitTo){

    $query	= "SELECT C.UNIT_NAME,
                      C.UNIT_TO
               FROM ".$SourceDb->fetchValue("srcDbMstModel")."      M,
                    ".$SourceDb->fetchValue("srcDbMstConvertion")." C
               WHERE	M.MODEL='".$modelName."'
                      AND C.MODEL_ID=M.MODEL_ID ";

    $Db->execFetchQuery($query,
                        $nrows,
                        $result);

    $nrowsFrom = sizeof($result["UNIT_NAME"]);

    if($nrowsFrom > 0){

      $unitFrom = $result["UNIT_NAME"];
    }else{

      $unitFrom = array();
    }

    $nrowsTo = sizeof($result["UNIT_TO"]);

    if($nrowsTo > 0){

      $unitTo   = $result["UNIT_TO"];
    }else{

      $unitTo = array();
    }
  }

  /**
  * fetch the header info, TR_DATE
  *
  * @param  string    $COracle
  * @param  string    $jobId
  * @param  string    $srcDb
  * @param  string    $resHeader
  *
  */
  function fetchTxHeader(&$COracle,
                         &$jobId,
                         &$srcDb,
                         &$resHeader){

    $query  = "SELECT TR_DATE
               FROM ".$srcDb."
               WHERE  JOB_ID='".$jobId."' ";

    $COracle->execFetchQuery($query,
                             $nrows,
                             $resHeader);
  }

  /**
  * fetch the detail info
  *
  * @param  string    $COracle
  * @param  string    $jobId
  * @param  string    $idTx
  * @param  string    $srcDb
  * @param  string    $resDetail
  *
  */
  function fetchTxDetail(&$COracle,
                         &$jobId,
                         &$idTx,
                         &$srcDb,
                         &$resDetail){

    $query  = "SELECT *
               FROM ".$srcDb."
               WHERE  JOB_ID='".$jobId."'
               ORDER BY NO ASC ";

    $COracle->execFetchQuery($query,
                             $nrows,
                             $resDetail);
  }

  /**
  * fetch the category name
  *
  * @param  string    $COracle
  * @param  string    $iCategory
  * @param  string    $srcDb
  * @param  string    $resCategory
  *
  */
  function fetchCategory(&$COracle,
                         &$iCategory,
                         &$srcDb,
                         &$resCategory){

    $query  = "SELECT CAT_NAME
               FROM ".$srcDb."
               WHERE  CAT_ID='".$iCategory."' ";

    $COracle->execFetchQuery($query,
                             $nrows,
                             $result);

    $resCategory = $result["CAT_NAME"][0];
  }

  /**
  * fetch the group name
  *
  * @param  string    $COracle
  * @param  string    $iGroup
  * @param  string    $srcDb
  * @param  string    $resGroup
  *
  */
  function fetchGroup( &$COracle,
                       &$iGroup,
                       &$srcDb,
                       &$resGroup){

    $query  = "SELECT GRP_NAME
               FROM ".$srcDb."
               WHERE  GRP_ID='".$iGroup."' ";

    $COracle->execFetchQuery($query,
                             $nrows,
                             $result);

    $resGroup = $result["GRP_NAME"][0];
  }


  /**
  * fetch rate of currency
  *
  * @param  string    $Db
  * @param  string    $srcSymbol
  * @param  string    $srcDb
  * @param  integer   $theRate
  *
  */
  function fetchRate(&$Db,
                     &$srcSymbol,
                     &$srcDb,
                     &$theRate){

    $query  = "SELECT CRR_RATE ".
              "FROM ".$srcDb." ".
              "WHERE  CRR_SYMBOL='".$srcSymbol."'
                      AND STATUS='"._ACTIVE."' ";

    $Db->execFetchQuery($query,
                        $nrows,
                        $result);

    $theRate = $result["CRR_RATE"][0];
  }

  /**
   * fetch rate of currency with
   * rate convertion handling
   *
   * @param Database  $Db
   * @param string    $srcSymbol
   * @param string    $toSymbol
   * @param string    $date
   * @param Composer  $SourceDb
   * @param string    $theRate
   */
  function fetchRate2(&$Db,
                       $srcSymbol,
                       $toSymbol,
                       $date,
                       $SourceDb,
                      &$theRate){

    if($date){

      $date = " AND CRR_DATE=TO_DATE('".$date."','dd-mm-YYYY')";
    }else{

      $date = " ";
    }

    $query  = "SELECT CRR_RATE ".
              "FROM ".$SourceDb->fetchValue("srcDbRate")." ".
              "WHERE  CRR_SYMBOL='".$toSymbol."'
                      ".$date."
                      AND STATUS='"._ACTIVE."' ";

    $Db->execFetchQuery($query,
                        $nrows,
                        $result);

    $theRate = $result["CRR_RATE"][0];

    if($srcSymbol == $toSymbol){

      $theRate = 1;
    }else{

      $query  = "SELECT HANDLE
                 FROM ".$SourceDb->fetchValue("srcDbRateHandling")."
                 WHERE  FROM_SYMBOL='".$srcSymbol."'
                        AND TO_SYMBOL='".$toSymbol."' ";

      $Db->execFetchQuery($query,
                          $nrows,
                          $result);

      switch($result["HANDLE"][0]){

        case _CURRENCY_HANDLE_MUL:

          $theRate *= 1;
          break;

        case _CURRENCY_HANDLE_DIV:

          $theRate = round((1/$theRate), 2);
          break;

        default:
      }
    }
  }

  /**
  * Fetch list of currency
  *
  * @param  string    $COracle
  * @param  string    $srcDb
  * @param  array     $lstCurrency
  *
  */
  function fetchLstCurrency(&$COracle,
                            &$srcDb,
                            &$lstCurrency){

    $query  = "SELECT CRR_SYMBOL ".
              "FROM ".$srcDb." ".
              "ORDER BY CRR_SYMBOL ASC ";

    $COracle->execFetchQuery($query,
                             $nrows,
                             $result);

    $lstCurrency = $result["CRR_SYMBOL"];
  }

  /**
  * Fetch list of warehouse
  *
  * @param  string    $COracle
  * @param  string    $srcDb
  * @param  array     $lstWarehouse
  *
  */
  function fetchLstWarehouse(&$COracle,
                             &$srcDb,
                             &$lstWarehouse){

    $query  = "SELECT WHS_NAME
               FROM ".$srcDb."
               WHERE  STATUS='"._ACTIVE."' ";

    $COracle->execFetchQuery($query,
                             $nrows,
                             $result);

    $lstWarehouse = $result["WHS_NAME"];
  }

  /**
  * fetch how many of nrows do we need to handle
  *
  * @param  string  $COracle
  * @param  string  $jobId
  * @param  string  $srcDb
  * @param  integer $maxTxId
  *
  */
  function fetchDtlNRows(&$COracle,
                         &$jobId,
                         &$srcDb,
                         &$maxTxId){

    $query  = "SELECT *
               FROM ".$srcDb."
               WHERE  JOB_ID='".$jobId."' ";

    $COracle->execFetchQuery($query,
                             $maxTxId,
                             $result);
  }

  /**
  * fetch the ID of category by category name
  *
  * @param  string    $COracle
  * @param  string    $nmCategory
  * @param  string    $srcDb
  * @param  string    $idCategory
  *
  */
  function fetchIdCategory(&$COracle,
                           &$nmCategory,
                           &$srcDb,
                           &$idCategory){

    $query = "SELECT CAT_ID
              FROM ".$srcDb."
              WHERE  CAT_NAME='".$nmCategory."' ";

    $COracle->execFetchQuery($query,
                             $nrows,
                             $result);

    $idCategory = $result["CAT_ID"][0];
  }

  /**
  * fetch the ID of group by group name
  *
  * @param  string    $COracle
  * @param  string    $nmGroup
  * @param  string    $srcDb
  * @param  string    $idGroup
  *
  */
  function fetchIdGroup(&$COracle,
                        &$nmGroup,
                        &$srcDb,
                        &$idGroup){

    $query = "SELECT GRP_ID
              FROM ".$srcDb."
              WHERE  GRP_NAME='".$nmGroup."' ";

    $COracle->execFetchQuery($query,
                             $nrows,
                             $result);

    $idGroup = $result["GRP_ID"][0];
  }

  /**
  * fetch how many details do we have
  *
  * @param  string  $COracle
  * @param  string  $jobId
  * @param  integer $noDetail
  * @param  string  $srcDb
  * @param  string  $nDetail
  * @param  string  $noDetail
  *
  */
  function fetchNDetail(&$COracle,
                        &$jobId,
                        &$srcDb,
                        &$nDetail,
                        &$noDetail,
                        &$lstDetail){

    $query  = "SELECT *
               FROM ".$srcDb."
               WHERE  JOB_ID='".$jobId."'
               ORDER BY NO ASC ";

    $COracle->execFetchQuery($query,
                             $nDetail,
                             $lstDetail);

    $noDetail   = $lstDetail["NO"];
  }

  /**
  * fetch all sub detail
  *
  * @param  string  $COracle
  * @param  string  $jobId
  * @param  integer $noDetail     source of detail number
  * @param  string  $srcDb
  * @param  string  $nSub
  * @param  string  $subDetail
  *
  */
  function fetchNSubDetail(&$COracle,
                           &$jobId,
                           &$noDetail,
                           &$srcDb,
                           &$nSub,
                           &$subDetail){

    $query  = "SELECT *
               FROM ".$srcDb."
               WHERE  JOB_ID='".$jobId."'
                      AND DTL_NO=".$noDetail." ";

    $COracle->execFetchQuery($query,
                             $nSub,
                             $subDetail);
  }

  /**
  * fetch this model from inventory DB
  *
  * @param  string  $COracle
  * @param  string  $model
  * @param  string  $modelId
  * @param  string  $unit
  * @param  string  $srcDb
  * @param  string  $isExists
  * @param  string  $resPrevious
  *
  */
  function fetchInvModel( &$COracle,
                          &$model,
                          &$modelId,
                          &$unit,
                          &$srcDb,
                          &$isExists,
                          &$resPrevious){

    $query  = "SELECT NO,
                      QTY
               FROM ".$srcDb."
               WHERE  MODEL='".$model."'
                      AND MODEL_ID='".$modelId."'
                      AND UNIT='".$unit."' ";

    $COracle->execFetchQuery($query,
                             $nrows,
                             $resPrevious);

    if($nrows > 0){

      $isExists = true;
    }else{

      $isExists = false;
    }
  }

  /**
  * fetch inventory last number
  *
  * @param  string  $COracle
  * @param  string  $srcDb
  * @param  string  $lastNumber
  *
  */
  function fetchLastNumber(&$COracle,
                           &$srcDb,
                           &$lastNumber){

    $query  = "SELECT NO
               FROM ".$srcDb."
               ORDER BY NO DESC ";

    $COracle->execFetchQuery($query,
                             $nrows,
                             $result);
    if($nrows > 0){

      $lastNumber = $result["NO"][0];
    }else{

      $lastNumber = 1; /* Default 1st number */
    }
  }

  /**
  * Fetch the netto
  *
  * @param  string  $COracle
  * @param  string  $jobId
  * @param  string  $srcDb
  * @param  string  $payable
  *
  */
  function fetchNetto( &$COracle,
                       &$jobId,
                       &$srcDb,
                       &$payable){

    $query  = "SELECT PAYABLE
               FROM ".$srcDb."
               WHERE  JOB_ID='".$jobId."' ";

    $COracle->execFetchQuery($query,
                             $nrows,
                             $result);

    $payable = $result["PAYABLE"][0];
  }

  /**
  * Fetch the prepaid
  *
  * @param  string  $COracle
  * @param  string  $jobId
  * @param  string  $srcDb
  * @param  string  $prepaid
  *
  */
  function fetchPrepaidDtl(&$COracle,
                           &$jobId,
                           &$srcDb,
                           &$prepaid,
                           &$currency,
                           &$rate){

    $query  = "SELECT PREPAID,
                      CURRENCY,
                      RATE
               FROM ".$srcDb."
               WHERE  JOB_ID='".$jobId."' ";

    $COracle->execFetchQuery($query,
                             $nrows,
                             $result);

    $prepaid = $result["PREPAID"][0];
  }

  /**
  * fetch the list of valid transactions
  *
  * @param  string    $COracle
  * @param  string    $condt      the condition
  * @param  string    $srcDb
  * @param  string    $lstResult
  *
  */
  function fetchLstTransaction(&$COracle,
                               $condt,
        	                     &$srcDb,
        	                     &$nResult,
        	                     &$lstResult){

    $query  = "SELECT *
               FROM ".$srcDb."
               ".$condt."
               ORDER BY TR_DATE DESC ";

    $COracle->execFetchQuery($query,
                             $nResult,
                             $lstResult);
  }

  /**
  * fetch what warehouse from header info
  *
  * @param  string  $COracle
  * @param  string  $jobId
  * @param  string  $srcDb
  * @param  string  $warehouse
  *
  */
  function fetchWarehouse( &$COracle,
                           &$jobId,
                           &$srcDb,
                           &$warehouse){

    $query  = "SELECT WAREHOUSE
               FROM ".$srcDb."
               WHERE  JOB_ID='".$jobId."' ";

    $COracle->execFetchQuery($query,
                             $nrows,
                             $result);

    $warehouse = $result["WAREHOUSE"][0];
  }

  /**
  * Fetch inventory balance
  *
  * @param  string  $COracle
  * @param  string  $model
  * @param  string  $modelId
  * @param  string  $unit
  * @param  string  $srcDb
  * @param  string  $qty
  *
  */
  function fetchInvBalance(&$COracle,
                           &$model,
                           &$modelId,
                           &$unit,
                           &$srcDb,
                           &$qty){

    $query  = "SELECT QTY
               FROM ".$srcDb."
               WHERE  MODEL='".$model."'
                      AND MODEL_ID='".$modelId."'
                      AND UNIT='".$unit."' ";

    $COracle->execFetchQuery($query,
                             $nrows,
                             $result);

    $qty  = $result["QTY"][0];
  }

  /**
  * Check if the model history is clean
  *
  * @param  string  $COracle
  * @param  string  $recvId
  * @param  integer $noDetail
  * @param  string  $model
  * @param  integer $modelId
  * @param  string  $unit
  * @param  string  $serNumber
  * @param  string  $srcDb
  *
  */
  function checkCleanModel($COracle,
                           $recvId,
                           $noDetail,
                           $model,
                           $modelId,
                           $unit,
                           $serNumber,
                           $srcDb,
                           $isClean){

    $query  = "SELECT NO
               FROM ".$srcDb."
               WHERE  SRC_REF='".$recvId."'
                      AND SRC_NO=".$noDetail."
                      AND MODEL='".$model."'
                      AND MODEL_ID='".$modelId."'
                      AND UNIT='".$unit."'
                      AND SER_NUMBER='".$serNumber."'
               ORDER BY NO DESC ";

    $COracle->execFetchQuery($query,
                             $nrows,
                             $result);

    if($nrows == 1){

      $isClean = false;
    }else{

      $isClean = true;
    }
  }

  /**
  * fetch last number of model history
  *
  * @param  string  $COracle
  * @param  string  $model
  * @param  integer $modelId
  * @param  string  $unit
  * @param  integer $serNumber
  * @param  string  $srcDb
  * @param  string  $lastNumber
  *
  */
  function fetchLastNoHistory(&$COracle,
                              &$model,
                              &$modelId,
                              &$unit,
                              &$serNumber,
                              &$srcDb,
                              &$lastNumber){

    $query = "SELECT  NO
              FROM ".$srcDb."
              WHERE   MODEL='".$model."'
                      AND MODEL_ID='".$modelId."'
                      AND UNIT='".$unit."'
                      AND SER_NUMBER='".$serNumber."'
              ORDER BY NO DESC ";

    $COracle->execFetchQuery($query,
                             $nrows,
                             $result);

    if($nrows > 0){

      $lastNumber = $result["NO"][0];
    }else{

      $lastNumber = $lastNumber = 1; /* Default 1st number */
    }
  }

  /**
  * Fetch if this model id is serNumber active
  *
  * @param  string  $COracle
  * @param  string  $model
  * @param  integer $modelId
  * @param  string  $srcDb
  * @param  integer $isSerNumber
  *
  */
  function fetchIsSerNumber(&$COracle,
                            &$model,
                            &$modelId,
                             $srcDb,
                            &$isSerNumber){

    define('_IS_ACTIVE'     , 1);

    $query  = "SELECT SER_NUMBER
               FROM ".$srcDb."
               WHERE  MODEL='".$model."'
                      AND MODEL_ID='".$modelId."' ";

    $COracle->execFetchQuery($query,
                             $nrows,
                             $result);

    if($result["SER_NUMBER"][0] == _IS_ACTIVE){

      $isSerNumber = true;
    }else{

      $isSerNumber = false;
    }
  }

  /**
  * fetch the manual input detail
  *
  * @param  string  $COracle
  * @param  string  $jobId
  * @param  string  $model
  * @param  string  $srcDb
  * @param  string  $manInData
  *
  */
  function fetchManEntDetail(&$COracle,
                             &$jobId,
                             &$model,
                             &$srcDb,
                             &$manInData){

    $query  = "SELECT *
               FROM ".$srcDb."
               WHERE  JOB_ID='".$jobId."'
                      AND MODEL='".$model."'
               ORDER BY NO ASC ";

    $COracle->execFetchQuery($query,
                             $nrows,
                             $result);

    /**
    * we concat all the detail
    *
    */
    if($nrows > 0){

      for($iRow = 0; $iRow < $nrows; $iRow++){

        if($iRow == 0){

          $tmpData  = $result["SER_NUMBER"][$iRow];
        }else{

          $tmpData .= "\n" . $result["SER_NUMBER"][$iRow];
        }
      }

      $manInData = $tmpData;
    }else{

      $manInData = "";
    }
  }

  /**
  * fetch the manual input detail
  *
  * @param  string  $COracle
  * @param  string  $jobId
  * @param  string  $srcDb
  * @param  string  $nManInput
  * @param  string  $resManInput
  *
  */
  function fetchManInDtl( &$COracle,
                          &$jobId,
                          &$srcDb,
                          &$nManInput,
                          &$resManInput){

    $query  = "SELECT SER_NUMBER,
                      NO
               FROM ".$srcDb."
               WHERE  JOB_ID='".$jobId."'
               ORDER BY NO ASC ";

    $COracle->execFetchQuery($query,
                             $nManInput,
                             $resManInput);
  }

  /**
   * fetch the manual input serial number
   *
   * @param string  $Db
   * @param string  $jobId
   * @param string  $srcDb
   * @param integer $mnlRows
   * @param array   $mnlResult
   */
  function fetchManualKeyInJobIdData(&$Db,
  				                           &$jobId,
  				                           &$srcDb,
  				                           &$mnlRows,
  				                           &$mnlResult){

  	$query = "SELECT SER_NUMBER
  	          FROM ".$srcDb."
  	          WHERE  JOB_ID='".$jobId."'
  	          ORDER BY NO ASC";

  	$Db->execFetchQuery($query,
  	                    $mnlRows,
  	                    $mnlResult);
  }
}
?>